package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.AdminGetLogAffairsDto;
import com.example.medical.entity.pojo.*;
import com.example.medical.service.LogingService;
import com.example.medical.mapper.LogingMapper;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【loging】的数据库操作Service实现
* @createDate 2024-04-04 23:59:46
*/
@Service
public class LogingServiceImpl extends ServiceImpl<LogingMapper, Loging>
    implements LogingService{

    @Autowired
    private LogingMapper logingMapper;

    @Override
    public Result AdminGetLogAffairs(AdminGetLogAffairsDto dto) {
        dto.checkParam();
        IPage<Loging> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Loging> wrapper=new LambdaQueryWrapper<>();
        if (dto.getCategory()!=null) {
            wrapper.eq(Loging::getLogingAffairsCategory,dto.getCategory());
        }
        if (dto.getType()!=null) {
            wrapper.eq(Loging::getLogingType,dto.getType());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public boolean SaveLoging(Affairs checkAffairs, User user) {
        Loging loging=new Loging();
        loging= loging.builder()
                .logingAffairsCategory(checkAffairs.getAffairsCategory())
                .logingName(checkAffairs.getAffairsDeviceName())
                .logingUserNickStart(user.getUserNick())
                .logingUserIdStart(user.getUserId())
                .logingAffairsId(checkAffairs.getAffairsId())
                .saveTime(String.valueOf(LocalDateTime.now())).build();
        boolean checkSave = save(loging);
        if (!checkSave) {
            return false;
        }
        return true;
    }

    @Override
    public boolean UserNextTrue(User checkUser, Affairs checkAffairs) {
        Loging loging=new Loging();
        Loging checkLoging = getOne(Wrappers.<Loging>lambdaQuery().eq(Loging::getLogingAffairsId, checkAffairs.getAffairsId()));
        loging= loging.builder()
                .logingId(checkLoging.getLogingId())
                .logingUserIdEnd(checkUser.getUserId())
                .logingUserNickEnd(checkUser.getUserNick())
                .logingType(1).build();
        boolean checkUpdate = updateById(loging);
        if (!checkUpdate) {
            return false;
        }
        return true;
    }

    @Override
    public boolean LeadershipNextTrue(Leadership checkLeadership, Affairs checkAffairs) {
        Loging checkLoging = getOne(Wrappers.<Loging>lambdaQuery().eq(Loging::getLogingAffairsId, checkAffairs.getAffairsId()));
        Loging loging=new Loging();
        loging= loging.builder()
                .logingId(checkLoging.getLogingId())
                .logingLeadershipId(checkLeadership.getLeadershipId())
                .logingLeadershipNick(checkLeadership.getLeadershipNick())
                .logingType(1).build();
        boolean checkUpdate = updateById(loging);
        if (!checkUpdate) {
            return false;
        }
        return true;
    }

    @Override
    public boolean LeadershipNextFalse(Leadership checkLeadership, Affairs checkAffairs, String memo) {
        Loging checkLoging = getOne(Wrappers.<Loging>lambdaQuery().eq(Loging::getLogingAffairsId, checkAffairs.getAffairsId()));
        Loging loging=new Loging();
        loging= loging.builder()
                .logingId(checkLoging.getLogingId())
                .logingLeadershipId(checkLeadership.getLeadershipId())
                .logingLeadershipNick(checkLeadership.getLeadershipNick())
                .logingMemo(memo)
                .logingType(2).build();
        boolean checkUpdate = updateById(loging);
        if (!checkUpdate) {
            return false;
        }
        return true;
    }


}




